package cn.edu.zsc.blms.service.impl.array;

import cn.edu.zsc.blms.App;
import cn.edu.zsc.blms.domain.BaseEntity;
import cn.edu.zsc.blms.exception.EntityNotExistException;

public abstract class BaseServiceImpl<R extends BaseEntity> {
    protected final BaseEntity[] DATA;
    protected int idCounter;

    public BaseServiceImpl(){
        DATA = new BaseEntity[100];
    }

    /**获取用户输入的有效ID值，返回-1表示用户输入的ID值是无效的...*/
   protected BaseEntity getOne() throws EntityNotExistException{
       BaseEntity entity;
       try{
           int id = Integer.parseInt(App.input.nextLine());
           if(id >= idCounter || id < 0 || DATA[id] == null){
               throw new EntityNotExistException();
           }
           entity = DATA[id];
       }catch (NumberFormatException e){
           throw new EntityNotExistException("ID为整数，请输入整数值");
       }
       return entity;
   }
}
